//
//  AddMaintainViewController.m
//  EPBPro
//
//  Created by wucha on 2018/7/11.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "AddMaintainViewController.h"
#import "ResizeView.h"
#import "HttpEquipmentAction.h"

@interface AddMaintainViewController ()
@property (nonatomic,strong) NSString *equipmentCode;
@property (nonatomic,strong) UITextField *nameTF;
@property (nonatomic,strong) EquipmentInfo *equipmentInfo;
@property (nonatomic,strong) UITextField *moneyTF;

@end

@implementation AddMaintainViewController

-(id)initWithEquipmentCode:(EquipmentInfo*)equipmentInfo{
    if (self == [super init]) {
        self.equipmentInfo = equipmentInfo;
        self.equipmentCode = equipmentInfo.code;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if ([self.equipmentInfo.status isEqualToString:@"maintaining"]) {
        self.titleName = @"更新维修";
    }else{
        self.titleName = @"添加维修";

    }
    ResizeView *resizeView = [[ResizeView alloc] initWithFrame:CGRectMake(0, StatusBarHeight+44, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarHeight-44)];
    [self.view addSubview:resizeView];
    [resizeView addResizeSubview:[self getTitleResizeViewInfo:@"设备编码:" value:self.equipmentCode]];
    if ([self.equipmentInfo.status isEqualToString:@"maintaining"]) {
        [resizeView addResizeSubview:[self getNormalResizeViewInfo:@"维修描述:"]];
        [resizeView addResizeSubview:[self getMoneyResizeViewInfo:@"金额:"]];
    }else{
        [resizeView addResizeSubview:[self getNormalResizeViewInfo:@"问题描述:"]];

    }
    [resizeView addResizeSubview:[self getSubmitResizeViewInfo]];
}

-(ResizeViewInfo*)getNormalResizeViewInfo:(NSString*)title{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 44;
    resizeViewInfo.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).autoWidthRatio(0);
    [titleLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UITextField *nameTF = [UITextField new];
    nameTF.textColor = [UIColor grayColor];
    nameTF.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:nameTF viewKey:@"TF"];
    nameTF.sd_layout.leftSpaceToView(titleLabel, 10).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).rightSpaceToView(resizeViewInfo.view, 10);
    self.nameTF = nameTF;
    
    return resizeViewInfo;
}

-(ResizeViewInfo*)getMoneyResizeViewInfo:(NSString*)title{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 44;
    resizeViewInfo.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).autoWidthRatio(0);
    [titleLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UITextField *nameTF = [UITextField new];
    nameTF.textColor = [UIColor grayColor];
    nameTF.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:nameTF viewKey:@"TF"];
    nameTF.sd_layout.leftSpaceToView(titleLabel, 10).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).rightSpaceToView(resizeViewInfo.view, 10);
    self.moneyTF = nameTF;
    
    return resizeViewInfo;
}

-(ResizeViewInfo*)getTitleResizeViewInfo:(NSString*)title value:(NSString*)valueString{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 44;
    resizeViewInfo.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).autoWidthRatio(0);
    [titleLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UILabel *nameLabel = [UILabel new];
    nameLabel.textColor = [UIColor grayColor];
    nameLabel.font = [UIFont systemFontOfSize:16];
    nameLabel.text = valueString;
    [resizeViewInfo addSubview:nameLabel];
    nameLabel.sd_layout.leftSpaceToView(titleLabel, 10).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(150);
    
    return resizeViewInfo;
}

-(ResizeViewInfo*)getSubmitResizeViewInfo{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 60;
    
    UIButton *submitButton = [UIButton new];
    submitButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [submitButton setTitle:@"提交" forState:UIControlStateNormal];
    [submitButton addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
    [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    submitButton.backgroundColor = DefaultTenantColor;
    [resizeViewInfo addSubview:submitButton];
    submitButton.sd_layout.leftSpaceToView(resizeViewInfo.view, 40).topSpaceToView(resizeViewInfo.view, 20).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(SCREEN_WIDTH-80);
    
    return resizeViewInfo;
}

-(void)submitAction:(id)sender{
    if ([self.nameTF.text isEqualToString:@""]) {
        [self showMessage:@"请填入问题描述" autoDissmissAfterDelay:0];
        return;
    }
    MaintainActionInfo *maintainActionInfo = [[MaintainActionInfo alloc] init];
    maintainActionInfo.code = self.equipmentCode;
    maintainActionInfo.descriptionOne = self.nameTF.text;
    maintainActionInfo.location = @"";
    [self showWaitingViewWithMessage:@"提交中..." style:WaitingViewShowStyleFullSelfView];
    if ([self.equipmentInfo.status isEqualToString:@"maintaining"]) {
        maintainActionInfo.id = self.equipmentInfo.maintainId;
        [HttpEquipmentAction putUpdateEquipmentMaintain:maintainActionInfo block:^(id result, NSError *error) {
            [self dismissWaitingView];
            if (!error) {
                [self showMessage:@"修改成功" autoDissmissAfterDelay:0];
                [self performBlock:^{
                    [self.navigationController popViewControllerAnimated:YES];
                } afterDelay:0.8];
            }
        }];
    }else{
        [HttpEquipmentAction postAddEquipmentMaintain:maintainActionInfo block:^(id result, NSError *error) {
            [self dismissWaitingView];
            if (!error) {
                [self showMessage:@"添加成功" autoDissmissAfterDelay:0];
                [self performBlock:^{
                    [self.navigationController popViewControllerAnimated:YES];
                } afterDelay:0.8];
            }
        }];
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
